﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBMS.Common;
using CBMS.Interfaces;
using CBMS.Business;
using CBMS.Models;
using CBMS.Resources;
using CBMS.Objects;
namespace CBMS.View
{
    public partial class frmChangePassword : frmBase
    {
        private int _staffId;
        public static IStaffRepository staffRep { get; set; }
        private string strError = string.Empty;
        public frmChangePassword()
        {
          //  this._staffId = this.userLoginId;
            InitializeComponent();
        }
        public frmChangePassword(int staffId)
        {
            InitializeComponent();
            this._staffId = staffId;
            staffRep = new StaffRepository();
        }

        private void frmChangePassword_Load(object sender, EventArgs e)
        {
            Staff objStaff = staffRep.GetByID(this._staffId);
            if (objStaff != null)
            {
                txtUserName.Text = objStaff.user_name;
            }
            if (this.userLoginId == _staffId)
            {
                txtCurrentPass.Focus();
                txtCurrentPass.Select();
            }
            else
            {
                RemoveRow(tblMain, 1);
                RemoveRow(tblMain, 1);
                tblMain.Height = 66;
                grpMain.Height = tblMain.Height + 50;
                Point pSave = btnSave.Location;
                pSave.Y = grpMain.Height + 20;
                btnSave.Location = pSave;
                pSave = btnClose.Location;
                pSave.Y = grpMain.Height + 20;
                btnClose.Location = pSave;
                this.Height = grpMain.Height + 100;
                // btnSave.Location.X = groupBox1.Height + 10;
                txtNewPass.Focus();
                txtNewPass.Select();
            }

        }
        public void RemoveRow(TableLayoutPanel panel, int rowIndex)
        {
            panel.RowStyles.RemoveAt(rowIndex);
            for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
            {
                var control = panel.GetControlFromPosition(columnIndex, rowIndex);
                if (control != null)
                    panel.Controls.Remove(control);
            }

            for (int i = rowIndex + 1; i < panel.RowCount; i++)
            {
                for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
                {
                    var control = panel.GetControlFromPosition(columnIndex, i);
                    if (control != null)
                        panel.SetRow(control, i - 1);
                }
            }

            panel.RowCount--;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            
            if (!CheckValidationForm())
            {
                MessageBox.Show(this.strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
               
                Staff objStaff = staffRep.GetByID(this._staffId);
                objStaff.password = BaseFunctions.Encrypte(txtNewPass.Text.Trim());
                if (this.userLoginId == _staffId)//check valid password                        
                {
                    if (objStaff.password != BaseFunctions.Encrypte(txtCurrentPass.Text.Trim()))
                    {
                        MessageBox.Show(string.Format(Resources.ErrorMessages.E0003, BaseFunctions.Removetext(lblCurrentPass.Text)), ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }                
                staffRep.SaveChanges();
                this.Close();
            }
        }
        private bool CheckValidationForm()
        {
            bool result = true;
            Control txt = new Control();
            string strNameText = string.Empty;
            string strAsign = "erro";
            string strErr = ErrorMessages.E0001;
            this.strError = string.Empty;
            Control ctrFocus = new Control();
            if (this.userLoginId == _staffId)
            {
                if (txtCurrentPass.Text == string.Empty)
                {
                    if (strNameText == string.Empty)
                    {
                        strNameText = strAsign;
                        txt = (Control)txtCurrentPass;
                    }
                    ctrFocus = (Control)txtCurrentPass;
                    this.strError += string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblCurrentPass.Text)) + "\r\n";
                    result = false;
                }
                if (txtConfirmCurrent.Text == string.Empty)
                {
                    if (strNameText == string.Empty)
                    {
                        strNameText = strAsign;
                        txt = (Control)txtConfirmCurrent;
                    }
                    this.strError += string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblConfirmCurrent.Text)) + "\r\n";
                    if (result)
                    {
                        ctrFocus = (Control)txtConfirmCurrent;
                        result = false;
                    }
                }
                if (txtConfirmCurrent.Text != string.Empty && txtCurrentPass.Text != string.Empty)
                {
                    if (txtConfirmCurrent.Text != txtCurrentPass.Text)
                    {
                        if (strNameText == string.Empty)
                        {
                            strNameText = strAsign;
                            txt = (Control)txtConfirmCurrent;
                        }
                        this.strError += string.Format(ErrorMessages.E0006, BaseFunctions.Removetext(lblConfirmCurrent.Text), BaseFunctions.Removetext(lblCurrentPass.Text)) + "\r\n";
                        if (result)
                        {
                            ctrFocus = (Control)txtConfirmCurrent;
                            result = false;
                        }
                    }
                }
            }
            if (txtNewPass.Text == string.Empty)
            {
                if (strNameText == string.Empty)
                {
                    strNameText = strAsign;
                    txt = (Control)txtNewPass;
                }
                this.strError += string.Format(ErrorMessages.E0001, BaseFunctions.Removetext(lblNewPass.Text)) + "\r\n";
                if (result)
                    ctrFocus = (Control)txtNewPass;
                result = false;
            }
            //set focus at postition first error
            BaseFunctions.SetFocusWhenError(ctrFocus, result, strNameText);
            return result;
        }
    }
}
